package com.ruoyi.security.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.security.config.DeviceEnums;
import com.ruoyi.security.domain.vo.DeviceInfoVo;
import com.ruoyi.security.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.security.domain.AfMaintenance;
import com.ruoyi.security.service.IAfMaintenanceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 运行维护Controller
 *
 * @author cjie
 * @date 2022-04-15
 */
@Api(tags = " 运行维护")
@RestController
@RequestMapping("/afMaintenance")
public class AfMaintenanceController extends BaseController {
    @Autowired
    private IAfMaintenanceService afMaintenanceService;

    @Autowired
    private IDeviceService deviceService;
    /**
     * 查询运行维护列表
     */
    @ApiOperation(value="查询运行维护列表", notes="查询运行维护列表")
    @PreAuthorize(hasPermi = "security:afMaintenance:list")
    @GetMapping("/list")
    public TableDataInfo list(AfMaintenance afMaintenance) {
        startPage();
        List<AfMaintenance> list = afMaintenanceService.selectAfMaintenanceList(afMaintenance);
        return getDataTable(list);
    }

    /**
     * 导出运行维护列表
     */
    @ApiOperation(value="导出运行维护列表", notes="导出运行维护列表")
    @PreAuthorize(hasPermi = "security:afMaintenance:export")
    @Log(title = "运行维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfMaintenance afMaintenance) throws IOException {
        List<AfMaintenance> list = afMaintenanceService.selectAfMaintenanceList(afMaintenance);
        ExcelUtil<AfMaintenance> util = new ExcelUtil<AfMaintenance>(AfMaintenance.class);
        util.exportExcel(response, list, "运行维护数据");
    }
    /**
     * 获取运行维护详细信息
     */
    @ApiOperation(value="获取运行维护详细信息", notes="获取运行维护详细信息")
    @PreAuthorize(hasPermi = "security:afMaintenance:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afMaintenanceService.selectAfMaintenanceById(id));
    }


    @ApiOperation(value="根据类型获取所有设备", notes="根据类型获取所有设备")
    @PreAuthorize(hasPermi = "security:afMaintenance:query")
    @GetMapping(value = "/type/{type}")
    public AjaxResult getDeviceByType(@PathVariable("type") String type) {
        List<DeviceInfoVo> list = new ArrayList<>();
        if (type.equals(DeviceEnums.ELEVATOR.getType())){
            //电梯设备
           list =  deviceService.selectElevators();
        }else if (type.equals(DeviceEnums.NYTRANS.getType())){
            list = deviceService.selectNyTrans();
        }
        for (DeviceInfoVo deviceInfoVo : list) {
            deviceInfoVo.setShowValue("== " + deviceInfoVo.getDeviceCode() + " == " + deviceInfoVo.getDeviceName() + " ==");
        }
        return AjaxResult.success(list);
    }


    /**
     * 新增运行维护
     */
    @ApiOperation(value="新增运行维护", notes="新增运行维护")
    @PreAuthorize(hasPermi = "security:afMaintenance:add")
    @Log(title = "运行维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfMaintenance afMaintenance) {
        return toAjax(afMaintenanceService.insertAfMaintenance(afMaintenance));
    }

    /**
     * 修改运行维护
     */
    @ApiOperation(value="修改运行维护", notes="修改运行维护")
    @PreAuthorize(hasPermi = "security:afMaintenance:edit")
    @Log(title = "运行维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfMaintenance afMaintenance) {
        return toAjax(afMaintenanceService.updateAfMaintenance(afMaintenance));
    }

    /**
     * 删除运行维护
     */
    @ApiOperation(value="删除运行维护", notes="删除运行维护")
    @ApiImplicitParam(name = "ids", value = "运行维护ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afMaintenance:remove")
    @Log(title = "运行维护", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afMaintenanceService.deleteAfMaintenanceByIds(ids));
    }
}
